/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import bean.User;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author tea milandi
 */
@WebServlet(name = "FilterLogin", urlPatterns = {"/FilterLogin"})
public class FilterLogin extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        Cookie ck1 = new Cookie("data1", username);
        response.addCookie(ck1);
        if (user.bandingUser() == true) {
            User user1 = new User();
            user1.setUsername(username);
            if ("admin".equals(user1.bandingJenisUser())) {
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("HomePusat.jsp");
                requestDispatcher.forward(request, response);
            } else if ("cabang".equals(user1.bandingJenisUser())) {
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("HomeCabang.jsp");
                requestDispatcher.forward(request, response);
            } else {
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("Login.html");
                requestDispatcher.forward(request, response);
            }
        } else {
            PrintWriter out = response.getWriter();
            out.println("<html>\n"
                    + "    <head>\n"
                    + "        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
                    + "        <title>Form Login</title>\n"
                    + "    </head>\n"
                    + "    <body bgcolor=\"FFC373\"> \n"
                    + "    <center>\n"
                    + "        <br><br><br><br><br><br><br><br>\n"
                    + "        <form action=\"FilterLogin\">\n"
                    + "            <table border=\"0\">\n"
                    + "                <tr>\n"
                    + "                    <td colspan=\"3\"><img src=\"gambar/logo asmart.png\" width=\"300\" height=\"84\"/></td>\n"
                    + "                </tr>\n"
                    + "<tr><td colspan=\"3\" align=\"center\"><font color=\"red\" size=\"3px\">Username/Password Salah atau Tidak Lengkap</font></td></tr>"
                    + "                <tr>\n"
                    + "                    <td align=\"center\"><font face=\"Berlin Sans FB Demi\">USERNAME</font></td>\n"
                    + "                    <td width=\"5\"></td>\n"
                    + "                    <td align=\"center\"><input type=\"text\" name=\"username\"/></td> \n"
                    + "                </tr>\n"
                    + "                <tr>\n"
                    + "                    <td align=\"center\"><font face=\"Berlin Sans FB Demi\">PASSWORD</font></td>\n"
                    + "                    <td width=\"8\"></td>\n"
                    + "                    <td align=\"center\"><input type=\"password\" name=\"password\"/></td> \n"
                    + "                </tr>\n"
                    + "                <tr>\n"
                    + "                    <td colspan=\"3\" align=\"center\"><input type=\"image\" src=\"gambar/login_button.png\" name=\"submit\" width=\"100\" height=\"30\"/></td>\n"
                    + "                </tr>\n"
                    + "            </table>\n"
                    + "        </form>\n"
                    + "    </center>\n"
                    + "</body>\n"
                    + "</html>");
//            RequestDispatcher requestDispatcher = request.getRequestDispatcher("Login.html");
//            requestDispatcher.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
